package com.kili.file.handler;

import com.kili.file.controller.vo.BaseResponse;
import com.kili.file.enums.ResponseCode;
import com.kili.file.exception.FileUploadException;
import com.kili.file.exception.FileValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * GlobalExceptionHandler
 *
 * @author Gordon
 * @version 2019/4/1
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = FileValidationException.class)
    public ResponseEntity fileValidationExceptionHandler(FileValidationException e) {
        log.error("FileValidationException ", e);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(BaseResponse.of(ResponseCode.INVALID_REQUEST));
    }

    @ExceptionHandler(value = FileUploadException.class)
    public ResponseEntity fileUploadExceptionHandler(FileUploadException e) {
        log.error("FileUploadException ", e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(BaseResponse.of(ResponseCode.UPLOAD_ERROR));
    }

        @ExceptionHandler(value = Exception.class)
        public ResponseEntity otherExceptionHandler(Exception e) {
            log.error("otherExceptionHandler ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(BaseResponse.of(ResponseCode.INTERNAL_ERROR));
        }
}
